/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : May 18, 2012  12:09:43 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.rental.receipt.misc
 * File Name : ReceiptMisc.java
 * TODO
 */
package apps.business.process.rental.receipt.misc;

import java.util.List;

import jpa.entitys.synchro.staging.FiArinvoicereceOutb;

import org.apache.log4j.Logger;

import promis.utility.CommonService;

import sap.business.fi.outbound.FiArinvoicereceOutbService;
import apps.business.process.SyncProcessorCommons;
import apps.business.process.rental.credit.note.CreditNoteProcessing;
import apps.promis.out.common.VwSyncRcvdAllAction;

/**
 * @author chatree
 *
 */
public class ReceiptMiscProcessing extends SyncProcessorCommons {
	
	private static final Logger log = Logger.getLogger(ReceiptMiscProcessing.class);

	/**
	 * 
	 */
	public ReceiptMiscProcessing() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 		String company = "SP"; 
			String project = "SPLVILLE-PRAGSA";
			String documentNo = "80-1205-000011"; 
			String status = "A";		
			String syncIsstaging = "N";
	 * @param company
	 * @param project
	 * @param documentNo
	 * @param status
	 * @param syncIsstaging
	 * @throws Exception
	 */
	public void Processing(String company, String project, String documentNo, String status, String syncIsstaging) throws Exception {
		try{
			
			//Create To Staging
			System.out.println("Find Receipt All company=" + company + " || project=" + project + " || documentNo=" + documentNo + " || status="+ status + " || syncIsstaging="+syncIsstaging);
			UPD_PROMISUseTime(true, false, null);
			List<FiArinvoicereceOutb> value_list = new VwSyncRcvdAllAction().CreateToStaging(company, project, documentNo, status, syncIsstaging);
			UPD_PROMISUseTime(false, true, null);			
			System.out.println("Create to Promis Receipt All company=" + company + " || project=" + project + " || documentNo=" + documentNo + " || status="+ status + " || syncIsstaging="+syncIsstaging +" || Size=" + CommonService.getSize(value_list));
			
			//Send to SAP
			if(CommonService.getSize(value_list) > 0){
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new FiArinvoicereceOutbService().SendToSAP(value_list);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}			
			System.out.println("Send to Promis Receipt All company=" + company + " || project=" + project + " || documentNo=" + documentNo + " || status="+ status + " || syncIsstaging="+syncIsstaging +" || Size=" + CommonService.getSize(value_list));
			
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
			log.error(e.getMessage());			
			throw e;
		}
	}

	@Deprecated
	private void Start() throws Exception {
		try{
						
			VwSyncRcvdAllAction obj = new VwSyncRcvdAllAction();
			String company = "SP";
			String project = "SPLVILLE-PRAGSA";
			String documentNo = "80-1205-000011"; 
			String status = "A";		
			String syncIsstaging = "N";
			
			//Create To Staging
			List<FiArinvoicereceOutb> value_list = obj.CreateToStaging(company, project, documentNo, status, syncIsstaging);
			
			//Send to SAP
			new FiArinvoicereceOutbService().SendToSAP(value_list);
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	@Deprecated
	private void StartReceiptMiscNotSyncInPromis() throws Exception {
		try{
						
			VwSyncRcvdAllAction obj = new VwSyncRcvdAllAction();						
			List<Object[]> lst = new VwSyncRcvdAllAction().SearchReceiptMiscNotSyncInPromis();
			System.out.println("SearchReceiptMiscNotSyncInPromis size=" + CommonService.getSize(lst));		
			
			if(CommonService.getSize(lst) > 0){
				for(Object[] value : lst){
					
					String company = "SP"; 
					String project = value[1].toString();
					String documentNo = value[2].toString(); 
					String status = "A";
					String syncIsstaging = "N";
					
					//Create To Staging
					List<FiArinvoicereceOutb> value_list = obj.CreateToStaging(company, project, documentNo, status, syncIsstaging);
					
					//Send to SAP
					new FiArinvoicereceOutbService().SendToSAP(value_list);
					System.out.println("Send to SAP size=" + CommonService.getSize(value_list));
					
				}				
			}			
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try{
			
			new ReceiptMiscProcessing().StartReceiptMiscNotSyncInPromis();
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}

	}

}
